package com.gimi.cloud.mmf.user.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.mmf.common.utils.JsonUtil;
import com.gimi.cloud.mmf.user.common.enums.ResponseCodeEnum;
import com.gimi.cloud.mmf.user.common.enums.ServiceNameEnum;
import com.gimi.cloud.mmf.user.common.reqhead.Response;
import com.gimi.cloud.mmf.user.common.reqhead.Util;
import com.gimi.cloud.mmf.user.service.TestUserService;

/**
 * Created by luck on 2017/09/28.
 */
@RestController
public class UserServiceConsumerController {

    private static Logger logger = LoggerFactory.getLogger(UserServiceConsumerController.class);

    @Autowired TestUserService testUserService;

    /**
     * 作为web服务的统一入口.
     *
     * @param requestData 请求参数
     * @param request     httpRequest
     * @param response    response
     */
    @RequestMapping(value = "service")
    public void userService(@RequestBody String requestData, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        logger.info("进入user微服务主程序入口.");
        logger.info("requestData:"+requestData);
        logger.info("authorization:" + request.getHeader("authorization"));
        logger.info("serviceName:" + request.getHeader("serviceName"));
        logger.info("userId:" + request.getHeader("userId"));

        // TODO: 2017/10/11 鉴权,确认是否有访问权限
        /*String powers = iRedisService.get("permission:userPermission:"+request.getHeader("userId"));
        Map powersMap = (Map) JsonUtil.jsonToBean(powers,Map.class);
        if(powersMap != null && powersMap.containsKey(request.getHeader("serviceName"))){
            logger.error("合法请求");
        }else{
            Response tmp_resp = new Response();
            tmp_resp.setResponseCode(ResponseCodeEnum.MANAGE_NO_POWER);
            Util.sendResponse(JsonUtil.objectToJson(tmp_resp),response);
        }*/

        Response resp = new Response();

        ServiceNameEnum serviceNameEnum = ServiceNameEnum.getStatus(request.getHeader("serviceName"));
        switch (serviceNameEnum) {
            case SAVEUSER:
            	testUserService.saveUser(requestData, response, request);
                break;
           
            /*********************系统配置结束***************************/
            default:
                resp.setResponseCode(ResponseCodeEnum.SERVICENAME_NOTFOUND);
                Util.sendResponse(JsonUtil.objectToJson(resp), response);
                break;
        }
        logger.info("执行完毕.");
    }

}
